/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Graphics2D;
import java.util.HashMap;

/**
 *
 * @author Jacob
 */
public class ETabFrame extends EFrame{
    private int tabHeight;
    private int frameHeight;
    private int buttonX;
    private EFrame currentTab;
    private HashMap<String, EFrame> tabs;
    private EButtonGroup tabGroup = new EButtonGroup();
    public ETabFrame(EComponent parent, int x, int y, int width, int tabHeight, int frameHeight)
    {
        super(parent, x, y, width, tabHeight+frameHeight);
        this.tabHeight = tabHeight;
        this.frameHeight = frameHeight;
        tabs = new HashMap<String, EFrame>(8);
    }
    public EButton addTab(final String name, final EFrame frame)
    {
        frame.setWidth(width);
        frame.setHeight(frameHeight);
        frame.setX(0);
        frame.setY(tabHeight);
        frame.setVisible(false);
        addChild(1, frame);
        EToggleButton tab = new EToggleButton(name);
        tab.setButtonGroup(tabGroup);
        tab.setX(buttonX);
        tab.setY(0);
        tab.setHeight(tabHeight);
        tab.setActionListener(new EActionListener()
        {

            @Override
            public void actionPerformed() {
                setTab(frame);
            }
            
        });
        addChild(1, tab);
        buttonX += tab.getWidth()+1;
        tabs.put(name, frame);
        if(name.equals("Basic"))
        {
            tab.down = true;
            tab.showEdges = true;
        }
        return tab;
    }
    
    public void addTab(final String name, final EFrame frame, final EActionListener action)
    {
        final EButton b = addTab(name, frame);
        final EActionListener old = b.actionListener;
        b.setActionListener(new EActionListener(){
            @Override
            public void actionPerformed() {
                old.actionPerformed();
                action.actionPerformed();
            }
        });
    }
    private void setTab(EFrame frame)
    {
        if(currentTab == frame)
            return;
        if(currentTab != null)
            currentTab.setVisible(false);
        currentTab = frame;
        currentTab.setVisible(true);
    }
    public void setTab(String name)
    {
        setTab(tabs.get(name));
    }
    @Override
    public void setHeight(int h)
    {
        super.setHeight(h);
        frameHeight = h - tabHeight;
        for(EFrame f : tabs.values())
        {
            f.setHeight(frameHeight);
        }
    }
    @Override
    public void render(Graphics2D g)
    {
        renderChildren(g);
    }
    @Override
    public void setVisible(boolean v)
    {
        visible = v;
        for(EFrame f : tabs.values())
        {
            if(f == currentTab)
                f.setVisible(v);
        }
    }
}
